
###################################################################
## TESTS
###################################################################

assert = (desc, expr) ->
  if ! expr
    console.log( " FAILED: #{desc}")
    throw new Error("Assertion failed")
  console.log( "  #{desc}")

AbleToSetAttributesIndividually = () ->

  x = o.div()
  x.SetContext()
  x.SetParentDOMElement( document.body )
  x.attr( "class", "myclass")

  el = x.GetDOMElement()

  assert(
    "Attributes get assigned"
    el.getAttribute("class") == "myclass"
  )

  x.attr( "class", null )
  assert(
    "Attributes can be removed"
    el.hasAttribute("class") == false
  )

AbleToSetAttributesIndividually()
AbleToSetAttributesFromObj = () ->

  x = o.div().text( "erge")
  x.SetContext()
  x.SetParentDOMElement( document.body )
  el = x.GetDOMElement()
  x.attr(
    class: "myclass",
    "background-style": "blue"
  )
  assert(
    "Attributes get assigned"
    el.getAttribute("class") == "myclass" and el.getAttribute("background-style") == "blue"
  )
  x.attr(
    class: null
    'background-style': null
  )
  assert(
    "attributes can be removed"
    el.hasAttribute( "class") == false and el.hasAttribute("background-style") == false
  )
AbleToSetAttributesFromObj()

StyleAssignmentsIncrementallyAddToTag = () ->

  x = o.div().text( "erge")
  x.SetContext()
  x.SetParentDOMElement( document.body )
  el = x.GetDOMElement()

  x.style( "background-color", "blue")
  x.style( "font-size", "14pt")

  assert(
    "Inline styles are additive"
    el.getAttribute( "style") == "background-color:blue;font-size:14pt;"
  )
  x.style( "font-size", null)
  assert(
    "and can be removed"
    el.getAttribute( "style" ) == "background-color:blue;"
  )

  # add a style that should be retained after set
  x.style( "font-size", "19pt")
  # assign a batch, that sets new values, clears an old one. check to make sure it retains previous
  x.style(
    "font-weight": "bold"
    "color" : "purple"
    "background-color": null
  )
  assert(
    "they can also be assigned as a batch without dropping old elements"
    el.getAttribute("style") == "font-size:19pt;font-weight:bold;color:purple;"
  )

StyleAssignmentsIncrementallyAddToTag()

EventsOnlyAllowSingleAssignment = () ->

  x = o.div().text( "erge")
  x.SetContext()
  x.SetParentDOMElement( document.body )
  el = x.GetDOMElement()

  x.attr( "class", "clickable" )
  abc = []
  x.on( "click", () -> abc.push "abc")
  x.on( "click", () -> abc.push 123 )

  evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  allowDefault = el.dispatchEvent(evt);
  assert(
    "(Simulated click) - hooked click event was overwritten and only responds to latest assignment"
    abc.length == 1 and abc[0] == 123
  )
  abc = []
  x.on( "click", null )

  evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

  assert(
    "removing an event works with null assignment"
    abc.length == 0
  )
EventsOnlyAllowSingleAssignment()


